﻿using System;
using System.Data;
using System.IO;
using System.Web.UI;
using BLL;
using DAL;
using Ext.Net;
using Roles;
using System.Configuration;

namespace ERP.Modules
{
    public partial class Employee : UserControl
    {
        #region[Define]
        private readonly string pathFolderRoot = ConfigurationManager.AppSettings["FolderRoot"];
        private static bool _flag;
        private DataTable _dtGetAutoID;
        private DataTable _dtGetEmpoyee;
        private DataTable _dtGetEmpoyeeEdit;
        private DataTable _dtGetBank;
        private DataTable _dtGetBankDetail;
        private DataTable _dtGetJobTitle;
        private DataTable _dtGetJobTitleDetail;
        private readonly BllEmployee _clsEmployee = new BllEmployee();
        private readonly Bank_BLL _clsBank = new Bank_BLL();
        private readonly Department_BLL _clsDepartment = new Department_BLL();
        private readonly Part_BLL _clsPart = new Part_BLL();
        private readonly National_BLL _clsNational = new National_BLL();
        private readonly Domain_BLL _clsDomain = new Domain_BLL();
        private readonly Province_BLL _clsProvince = new Province_BLL();
        private readonly Ethnic_BLL _clsEthnic = new Ethnic_BLL();
        private readonly Districts_BLL _clsDistricts = new Districts_BLL();
        private readonly Job_BLL _clsJobTitle = new Job_BLL();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                GetEmployee();
                GetNational();
                txtBrithday.Text = DateTime.Now.ToString();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
            }
        }
        //Store
        protected void StoreEmployee_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            GetEmployee();
        }
        protected void StoreBankBankEmpoyee_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearchBank.Text = string.Empty;
            GetBank();
        }
        protected void StoreJobEmployee_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearchJobTitle.Text = string.Empty;
            GetJobTitle();
        }
        protected void StoreEmployeeDepartment_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearchEmployeeDepartment.Text = string.Empty;
            GetDepartment();
        }
        protected void StoreEmployeePart_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearchEmployeePart.Text = string.Empty;
            GetPart();
        }
        protected void StoreEmployeeEthnic_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearchEmployeeEthnic.Text = string.Empty;
            GetEthnic();
        }
        protected void StoreEmployeeDistricts_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearchEmployeeDistricts.Text = string.Empty;
            SelectBoxEmployeeNational.Text = string.Empty;
            SelectBoxEmployeeDomain.Text = string.Empty;
            SelectBoxProvincesEmployee.Text = string.Empty;
            GetDistricts();
        }
        //Button
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            ClearText();
            GetAutoID();
            _flag = true;
            WindowsEmployee.Show();
            txtLastName.Focus(false, 100);
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grEmployee.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    _flag = false;
                    ClearText();
                    GetEmployeeEdit(int.Parse(row.RecordID));
                    WindowsEmployee.Show();
                }
        }
        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grEmployee.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    try
                    {
                        var obj = new EtEmployee { EmployeeId = int.Parse(row.RecordID) };
                        BllEmployee.Delete(obj);
                    }
                    catch (Exception)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                    }
                }
            GetEmployee();
        }
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            Insert();
        }
        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            WindowsEmployee.Close();
        }
        protected void linkGetCode_Click(object sender, DirectEventArgs e)
        {
            GetAutoID();
        }
        protected void SelectBoxPhoneType_Change(object sender, DirectEventArgs e)
        {
            if (SelectBoxPhoneType.SelectedItem.Value == "0")
            {
                txtCellPhone.Hidden = false;
                InputMaskCellPhone.Mask = "(9999) 999-999";
            }
            else
            {
                txtCellPhone.Hidden = false;
                txtCellPhone.Text = string.Empty;
                InputMaskCellPhone.Mask = "(99999) 999-999";
            }
        }
        protected void linkDepartment_Click(object sender, DirectEventArgs e)
        {
            GetDepartment();
            WindowEmployeeDepartment.Show();
        }
        protected void linkPart_Click(object sender, DirectEventArgs e)
        {
            GetPart();
            WindowsEmployeePart.Show();
        }
        protected void linkEthnic_Click(object sender, DirectEventArgs e)
        {
            GetEthnic();
            WindowEmployeeEthnic.Show();
        }
        protected void linkDistricts_Click(object sender, DirectEventArgs e)
        {
            hdIDDistricts_Id.Text = string.Empty;
            txtDistricts.Text = string.Empty;
            txtNational.Text = string.Empty;
            txtProvince.Text = string.Empty;
            txtDomain.Text = string.Empty;
            grEmployeeDistricts.GetStore().RemoveAll();
            SelectBoxEmployeeNational.Text = string.Empty;
            SelectBoxEmployeeDomain.Text = string.Empty;
            SelectBoxProvincesEmployee.Text = string.Empty;
            GetDistricts();
            WindowEmployeeDistricts.Show();
        }
        //Choose linkbutton
        protected void linkBank_Click(object sender, DirectEventArgs e)
        {
            txtSearchBank.Text = string.Empty;
            GetBank();
            WindowBank.Show();
        }
        protected void linkJobTitle_Click(object sender, DirectEventArgs e)
        {
            GetJobTitle();
            WindowJob.Show();
        }
        //Double click
        protected void EmployeeDblClick(object sender, DirectEventArgs e)
        {
            _flag = false;
            ClearText();
            int Employee_Id = int.Parse(e.ExtraParams["Employee_Id"]);
            GetEmployeeEdit(Employee_Id);
            WindowsEmployee.Show();
        }
        //Cell click
        protected void CellBank_Click(object sender, DirectEventArgs e)
        {
            var sm = grBankEmpoyee.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                var id = sm.SelectedCell.RecordID;
                GetBankDetail(int.Parse(id));
            }
            if (sm != null)
            {
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowBank.Close();
        }
        protected void CellJobEmployee_Click(object sender, DirectEventArgs e)
        {
            var sm = grJobEmployee.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                var id = sm.SelectedCell.RecordID;
                GetJobTitleDetail(int.Parse(id));
            }
            if (sm != null)
            {
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowJob.Close();
        }
        protected void CellEmployeeDepartment_Click(object sender, DirectEventArgs e)
        {
            var sm = grEmployeeDepartment.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                var id = sm.SelectedCell.RecordID;
                GetDepartmentDetail(int.Parse(id));
            }
            if (sm != null)
            {
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowEmployeeDepartment.Close();
        }
        protected void CellEmployeeEthnic_Click(object sender, DirectEventArgs e)
        {
            var sm = grEmployeeEthnic.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                var id = sm.SelectedCell.RecordID;
                GetEthnicDetail(int.Parse(id));
            }
            if (sm != null)
            {
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowEmployeeEthnic.Close();
        }
        protected void CellEmployeeDistricts_Click(object sender, DirectEventArgs e)
        {
            hdIDDistricts_Id.Text = string.Empty;
            txtDistricts.Text = string.Empty;
            txtNational.Text = string.Empty;
            txtProvince.Text = string.Empty;
            txtDomain.Text = string.Empty;
            var sm = grEmployeeDistricts.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                var id = sm.SelectedCell.RecordID;
                GetDistrictsDetail(int.Parse(id));
            }
            if (sm != null)
            {
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowEmployeeDistricts.Close();
        }
        protected void CellEmployeePart_Click(object sender, DirectEventArgs e)
        {
            var sm = grEmployeePart.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                var id = sm.SelectedCell.RecordID;
                GetPartDetail(int.Parse(id));
            }
            if (sm != null)
            {
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowsEmployeePart.Close();
        }
        //Search
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            DataTable dtSearch = _clsEmployee.GetEmployee(txtSearch.Text);
            if (dtSearch != null && dtSearch.Rows.Count > 0)
            {
                StoreEmployee.DataSource = dtSearch;
                StoreEmployee.DataBind();
            }
            else
            {
                grEmployee.GetStore().RemoveAll();
            }
        }
        protected void SearchBank_Event(object sender, DirectEventArgs e)
        {
            DataTable dtSearchBank = _clsBank.BindGirdtb_Bank(txtSearchBank.Text);
            if (dtSearchBank != null && dtSearchBank.Rows.Count > 0)
            {
                StoreBankBankEmpoyee.DataSource = dtSearchBank;
                StoreBankBankEmpoyee.DataBind();
            }
            else
            {
                grBankEmpoyee.GetStore().RemoveAll();
            }
        }
        protected void SearchJob_Event(object sender, DirectEventArgs e)
        {
            DataTable dtSearchJob = _clsJobTitle.BindGirdtb_Job(txtSearchJobTitle.Text);
            if (dtSearchJob != null && dtSearchJob.Rows.Count > 0)
            {
                StoreJobEmployee.DataSource = dtSearchJob;
                StoreJobEmployee.DataBind();
            }
            else
            {
                grJobEmployee.GetStore().RemoveAll();
            }
        }
        protected void EmployeeDepartment_Event(object sender, DirectEventArgs e)
        {
            var dtSearchDepartment = _clsDepartment.BindGirdtb_Department(txtSearchEmployeeDepartment.Text);
            if (dtSearchDepartment != null && dtSearchDepartment.Rows.Count > 0)
            {
                StoreEmployeeDepartment.DataSource = dtSearchDepartment;
                StoreEmployeeDepartment.DataBind();
            }
            else
            {
                grEmployeeDepartment.GetStore().RemoveAll();
            }
        }
        protected void EmployeePart_Event(object sender, DirectEventArgs e)
        {
            var dtSearchPart = _clsPart.BindGirdtb_Part(txtSearchEmployeePart.Text);
            if (dtSearchPart != null && dtSearchPart.Rows.Count > 0)
            {
                StoreEmployeePart.DataSource = dtSearchPart;
                StoreEmployeePart.DataBind();
            }
            else
            {
                grEmployeePart.GetStore().RemoveAll();
            }
        }
        protected void EmployeeEthnic_Event(object sender, DirectEventArgs e)
        {
            var dtSearchEthnic = _clsEthnic.BindGirdtb_Ethnic(txtSearchEmployeeEthnic.Text);
            if (dtSearchEthnic != null && dtSearchEthnic.Rows.Count > 0)
            {
                StoreEmployeeEthnic.DataSource = dtSearchEthnic;
                StoreEmployeeEthnic.DataBind();
            }
            else
            {
                grEmployeeEthnic.GetStore().RemoveAll();
            }
        }
        protected void EmployeeDistricts_Event(object sender, DirectEventArgs e)
        {
            var dtSearchDistricts = _clsDistricts.BindGirdDistricts(txtSearchEmployeeDistricts.Text);
            if (dtSearchDistricts != null && dtSearchDistricts.Rows.Count > 0)
            {
                StoreEmployeeDistricts.DataSource = dtSearchDistricts;
                StoreEmployeeDistricts.DataBind();
            }
            else
            {
                grEmployeeDistricts.GetStore().RemoveAll();
            }
        }
        //Select
        protected void SelectBoxEmployeeNational_Change(object sender, DirectEventArgs e)
        {
            SelectBoxEmployeeDomain.Text = string.Empty;
            SelectBoxProvincesEmployee.Text = string.Empty;
            grEmployeeDistricts.GetStore().RemoveAll();
            GetDomain();
        }
        protected void SelectBoxEmployeeDomain_Change(object sender, DirectEventArgs e)
        {
            SelectBoxProvincesEmployee.Text = string.Empty;
            grEmployeeDistricts.GetStore().RemoveAll();
            GetProvince();
        }
        protected void SelectBoxProvincesEmployee_Change(object sender, DirectEventArgs e)
        {
            GetDistricts();
        }
        //FileUpload
        protected void btnUpload_Click(object sender, DirectEventArgs e)
        {
            if (String.IsNullOrEmpty(FileUploadPanel.FileName))
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", "Không có tập tin được chọn !");
            }
            else
            {
                if (chkEmployee_Gender_Male.Checked)
                {
                    if (CheckFileNameExists("AvatarMan" + "/"))
                    {
                        HTML.clsFormat.Alert_ERROR("Lỗi",
                            "Tên tập tin này đã tồn tại trong thư mục " + "AvatarMan" +
                            " vui lòng đổi tên tập tin trước khi tải lên !");
                    }
                    else
                    {
                        Upload(Server.MapPath(pathFolderRoot + "AvatarMan" + "/"));
                    }
                }
                else
                {
                    if (CheckFileNameExists("AvatarWomen" + "/"))
                    {
                        HTML.clsFormat.Alert_ERROR("Lỗi",
                            "Tên tập tin này đã tồn tại trong thư mục " + "AvatarWomen" +
                            " vui lòng đổi tên tập tin trước khi tải lên !");
                    }
                    else
                    {
                        Upload(Server.MapPath(pathFolderRoot + "AvatarWomen" + "/"));
                    }
                }
            }
        }
        protected void btnCancel_Click(object sender, DirectEventArgs e)
        {
            FileUploadPanel.Text = string.Empty;
            WindowFileUpload.Close();
        }
        #endregion

        #region[Method]
        private void GetEmployee()
        {
            _dtGetEmpoyee = _clsEmployee.GetEmployee(txtSearch.Text);
            if (_dtGetEmpoyee != null && _dtGetEmpoyee.Rows.Count > 0)
            {
                StoreEmployee.DataSource = _dtGetEmpoyee;
                StoreEmployee.DataBind();
            }
            else
            {
                grEmployee.GetStore().RemoveAll();
            }
        }
        private void GetEmployeeEdit(int id)
        {
            _dtGetEmpoyeeEdit = _clsEmployee.GetEmployeeEdit(id);
            if (_dtGetEmpoyeeEdit != null && _dtGetEmpoyeeEdit.Rows.Count > 0)
            {
                hdIDEmployee.Text = _dtGetEmpoyeeEdit.Rows[0]["Employee_Id"].ToString();
                hdIDBank.Text = _dtGetEmpoyeeEdit.Rows[0]["Bank_Id"].ToString();
                hdIDJobTitle.Text = _dtGetEmpoyeeEdit.Rows[0]["Job_ID"].ToString();
                hdIDEthnic_Id.Text = _dtGetEmpoyeeEdit.Rows[0]["Ethnic_Id"].ToString();
                hdIDDistricts_Id.Text = _dtGetEmpoyeeEdit.Rows[0]["Districts_Id"].ToString();
                hdIDDepartment_Id.Text = _dtGetEmpoyeeEdit.Rows[0]["Department_Id"].ToString();
                hdIDPart_Id.Text = _dtGetEmpoyeeEdit.Rows[0]["Part_Id"].ToString();

                txtEmpCode.Text = _dtGetEmpoyeeEdit.Rows[0]["Bank_Code"].ToString();
                txtLastName.Text = _dtGetEmpoyeeEdit.Rows[0]["LastName"].ToString();
                txtFistName.Text = _dtGetEmpoyeeEdit.Rows[0]["FistName"].ToString();
                txtFullName.Text = _dtGetEmpoyeeEdit.Rows[0]["FullName"].ToString();
                txtBrithday.Text = _dtGetEmpoyeeEdit.Rows[0]["Brithday"].ToString();
                txtHomePhone.Text = _dtGetEmpoyeeEdit.Rows[0]["HomePhone"].ToString();
                
                txtBankName.Text = _dtGetEmpoyeeEdit.Rows[0]["Bank_Name"].ToString();
                txtJobtitle.Text = _dtGetEmpoyeeEdit.Rows[0]["Job_Name"].ToString();
                txtEmail.Text = _dtGetEmpoyeeEdit.Rows[0]["Email"].ToString();
                if (_dtGetEmpoyeeEdit.Rows[0]["Sex"].ToString() == "Nữ")
                {
                    chkEmployee_Gender.Checked = true;
                }
                else
                {
                    chkEmployee_Gender_Male.Checked = true;
                }
                txtAddress.Text = _dtGetEmpoyeeEdit.Rows[0]["Address"].ToString();
                ImgEmployee.ImageUrl = _dtGetEmpoyeeEdit.Rows[0]["Img"].ToString();
                txtPathImg.Text = _dtGetEmpoyeeEdit.Rows[0]["Img"].ToString();
                //FileUploadPanel.Text = _dtGetEmpoyeeEdit.Rows[0]["Img"].ToString();
                txtCMND.Text = _dtGetEmpoyeeEdit.Rows[0]["CMND"].ToString();
                txtPlaceCMND.Text = _dtGetEmpoyeeEdit.Rows[0]["PlaceCMND"].ToString();
                txtEthnic.Text = _dtGetEmpoyeeEdit.Rows[0]["Ethnic_Name"].ToString();
                txtReligion.Text = _dtGetEmpoyeeEdit.Rows[0]["Religion"].ToString();
                txtNational.Text = _dtGetEmpoyeeEdit.Rows[0]["National_Name"].ToString();
                txtDomain.Text = _dtGetEmpoyeeEdit.Rows[0]["Domain_Name"].ToString();
                txtProvince.Text = _dtGetEmpoyeeEdit.Rows[0]["Province_Name"].ToString();
                txtDistricts.Text = _dtGetEmpoyeeEdit.Rows[0]["Districts_Name"].ToString();
                txtDepartment.Text = _dtGetEmpoyeeEdit.Rows[0]["Department_Name"].ToString();
                txtPart.Text = _dtGetEmpoyeeEdit.Rows[0]["Part_Name"].ToString();
                if (_dtGetEmpoyeeEdit.Rows[0]["CellPhone"].ToString().Length <= 14)
                {
                    SelectBoxPhoneType.SelectedItem.Value = "0";
                    txtCellPhone.Hidden = false;
                    InputMaskCellPhone.Mask = "(9999) 999-999";
                    txtCellPhone.Text = _dtGetEmpoyeeEdit.Rows[0]["CellPhone"].ToString();
                    
                }
                else
                {
                    SelectBoxPhoneType.SelectedItem.Value = "1";
                    txtCellPhone.Hidden = true;
                    InputMaskCellPhone.Mask = "(99999) 999-999";
                    txtCellPhone.Text = _dtGetEmpoyeeEdit.Rows[0]["CellPhone"].ToString();
                }
                txtDateCreate.Text = _dtGetEmpoyeeEdit.Rows[0]["DateCreate"].ToString();
                if (_dtGetEmpoyeeEdit.Rows[0]["ObjectTimekeeping"].ToString() != string.Empty)
                {
                    if ((bool)_dtGetEmpoyeeEdit.Rows[0]["ObjectTimekeeping"])
                    {
                        chkObjectTimekeeping.Checked = true;
                    }
                    else
                    {
                        chkObjectTimekeeping.Checked = false;
                    }
                }  
                txtEducationalLevel.Text = _dtGetEmpoyeeEdit.Rows[0]["EducationalLevel"].ToString();
                txtLanguages.Text = _dtGetEmpoyeeEdit.Rows[0]["Languages"].ToString();
                txtSpecialize.Text = _dtGetEmpoyeeEdit.Rows[0]["Specialize"].ToString();
                txtInfomationLevel.Text = _dtGetEmpoyeeEdit.Rows[0]["InfomationLevel"].ToString();
                txtBHXH.Text = _dtGetEmpoyeeEdit.Rows[0]["BHXH"].ToString();
                txtDateCreateBHXH.Text = _dtGetEmpoyeeEdit.Rows[0]["DateCreateBHXH"].ToString();
                txtNumberOfVacationDays.Text = String.IsNullOrEmpty(_dtGetEmpoyeeEdit.Rows[0]["NumberOfVacationDays"].ToString()) ? "0" : _dtGetEmpoyeeEdit.Rows[0]["NumberOfVacationDays"].ToString();
                txtAccountNumber.Text = _dtGetEmpoyeeEdit.Rows[0]["AccountNumber"].ToString();
            }
        }
        private void GetBank()
        {
            _dtGetBank = _clsBank.BindGirdtb_Bank(txtSearchBank.Text);
            if(_dtGetBank !=null && _dtGetBank.Rows.Count>0)
            {
                StoreBankBankEmpoyee.DataSource = _dtGetBank;
                StoreBankBankEmpoyee.DataBind();
            }
            else
            {
                grBankEmpoyee.GetStore().RemoveAll();
            }
        }
        private void GetBankDetail(int id)
        {
            _dtGetBankDetail = _clsBank.BindGirdtb_BankDetail(id);
            if (_dtGetBankDetail != null && _dtGetBankDetail.Rows.Count > 0)
            {
                hdIDBank.Text = _dtGetBankDetail.Rows[0]["Bank_Id"].ToString();
                txtBankName.Text = _dtGetBankDetail.Rows[0]["Bank_Name"].ToString();
            }
        }
        private void GetJobTitle()
        {
            _dtGetJobTitle = _clsJobTitle.BindGirdtb_Job(txtSearchJobTitle.Text);
            if (_dtGetJobTitle != null && _dtGetJobTitle.Rows.Count > 0)
            {
                StoreJobEmployee.DataSource = _dtGetJobTitle;
                StoreJobEmployee.DataBind();
            }
            else
            {
                grJobEmployee.GetStore().RemoveAll();
            }
        }
        private void GetJobTitleDetail(int id)
        {
            _dtGetJobTitleDetail = _clsJobTitle.BindGirdtb_JobDetail(id);
            if (_dtGetJobTitleDetail != null && _dtGetJobTitleDetail.Rows.Count > 0)
            {
                hdIDJobTitle.Text = _dtGetJobTitleDetail.Rows[0]["Job_ID"].ToString();
                txtJobtitle.Text = _dtGetJobTitleDetail.Rows[0]["Job_Name"].ToString();
            }
        }
        private void GetDepartment()
        {
            var _dtGetDepartment = _clsDepartment.BindGirdtb_Department(txtSearchEmployeeDepartment.Text);
            if (_dtGetDepartment != null && _dtGetDepartment.Rows.Count > 0)
            {
                StoreEmployeeDepartment.DataSource = _dtGetDepartment;
                StoreEmployeeDepartment.DataBind();
            }
            else
            {
                grEmployeeDepartment.GetStore().RemoveAll();
            }
        }
        private void GetDepartmentDetail(int id)
        {
            var _dtGetDepartmentDetail = _clsDepartment.GetDataEdit(id);
            if (_dtGetDepartmentDetail != null && _dtGetDepartmentDetail.Rows.Count > 0)
            {
                hdIDDepartment_Id.Text = _dtGetDepartmentDetail.Rows[0]["Department_Id"].ToString();
                txtDepartment.Text = _dtGetDepartmentDetail.Rows[0]["Department_Name"].ToString();
            }
        }
        private void GetPart()
        {
            var _dtGetPart = _clsPart.BindGirdtb_Part(txtSearchEmployeePart.Text);
            if (_dtGetPart != null && _dtGetPart.Rows.Count > 0)
            {
                StoreEmployeePart.DataSource = _dtGetPart;
                StoreEmployeePart.DataBind();
            }
            else
            {
                grEmployeePart.GetStore().RemoveAll();
            }
        }
        private void GetPartDetail(int id)
        {
            var _dtGetPartDetail = _clsPart.BindGirdtb_PartDetail(id);
            if (_dtGetPartDetail != null && _dtGetPartDetail.Rows.Count > 0)
            {
                hdIDPart_Id.Text = _dtGetPartDetail.Rows[0]["Part_Id"].ToString();
                txtPart.Text = _dtGetPartDetail.Rows[0]["Part_Name"].ToString();
            }
        }
        private void GetEthnic()
        {
            var _dtGetEthnic = _clsEthnic.BindGirdtb_Ethnic(txtSearchEmployeeEthnic.Text);
            if (_dtGetEthnic != null && _dtGetEthnic.Rows.Count > 0)
            {
                StoreEmployeeEthnic.DataSource = _dtGetEthnic;
                StoreEmployeeEthnic.DataBind();
            }
            else
            {
                grEmployeeEthnic.GetStore().RemoveAll();
            }
        }
        private void GetEthnicDetail(int id)
        {
            var _dtGetEthnicDetail = _clsEthnic.BindGirdtb_EthnicDetail(id);
            if (_dtGetEthnicDetail != null && _dtGetEthnicDetail.Rows.Count > 0)
            {
                hdIDEthnic_Id.Text = _dtGetEthnicDetail.Rows[0]["Ethnic_ID"].ToString();
                txtEthnic.Text = _dtGetEthnicDetail.Rows[0]["Ethnic_Name"].ToString();
            }
        }
        private void GetDistricts()
        {
            if (String.IsNullOrEmpty(SelectBoxProvincesEmployee.Text))
            {
                var _dtGetDistricts = _clsDistricts.BindGirdDistricts(txtSearchEmployeeEthnic.Text);
                if (_dtGetDistricts != null && _dtGetDistricts.Rows.Count > 0)
                {
                    StoreEmployeeDistricts.DataSource = _dtGetDistricts;
                    StoreEmployeeDistricts.DataBind();
                }
                else
                {
                    grEmployeeDistricts.GetStore().RemoveAll();
                }
            }
            else
            {
                var _dtGetDistricts =
                    _clsDistricts.GetDistrictsFillter(int.Parse(SelectBoxProvincesEmployee.SelectedItem.Value));
                if (_dtGetDistricts != null && _dtGetDistricts.Rows.Count > 0)
                {
                    StoreEmployeeDistricts.DataSource = _dtGetDistricts;
                    StoreEmployeeDistricts.DataBind();
                }
                else
                {
                    grEmployeeDistricts.GetStore().RemoveAll();
                }
            }
        }
        private void GetDistrictsDetail(int id)
        {
            var _dtGetDistrictsDetail = _clsDistricts.GetDistrictsDetail(id);
            if (_dtGetDistrictsDetail != null && _dtGetDistrictsDetail.Rows.Count > 0)
            {
                hdIDDistricts_Id.Text = _dtGetDistrictsDetail.Rows[0]["Districts_Id"].ToString();
                txtDistricts.Text = _dtGetDistrictsDetail.Rows[0]["Districts_Name"].ToString();
                txtNational.Text = _dtGetDistrictsDetail.Rows[0]["National_Name"].ToString();
                txtDomain.Text = _dtGetDistrictsDetail.Rows[0]["Domain_Name"].ToString();
                txtProvince.Text = _dtGetDistrictsDetail.Rows[0]["Province_Name"].ToString();
            }
        }
        private void GetNational()
        {
            var _dtNational = _clsNational.BindGirdNational(string.Empty);
            if (_dtNational != null && _dtNational.Rows.Count > 0)
            {
                StoreEmployeeNational.DataSource = _dtNational;
                StoreEmployeeNational.DataBind();
            }
            else
            {
                SelectBoxEmployeeNational.GetStore().RemoveAll();
            }
        }
        private void GetDomain()
        {
            var _dtGetDomain = _clsDomain.GetDomainFillter(int.Parse(SelectBoxEmployeeNational.SelectedItem.Value));
            if (_dtGetDomain != null && _dtGetDomain.Rows.Count > 0)
            {
                StoreEmployeeDomain.DataSource = _dtGetDomain;
                StoreEmployeeDomain.DataBind();
            }
            else
            {
                SelectBoxEmployeeDomain.GetStore().RemoveAll();
            }
        }
        private void GetProvince()
        {
            var _dtGetProvince = _clsProvince.GetProvinceFillter(int.Parse(SelectBoxEmployeeDomain.SelectedItem.Value));
            if (_dtGetProvince != null && _dtGetProvince.Rows.Count > 0)
            {
                StoreProvincesEmployee.DataSource = _dtGetProvince;
                StoreProvincesEmployee.DataBind();
            }
            else
            {
                SelectBoxProvincesEmployee.GetStore().RemoveAll();
            }
        }
        private void GetAutoID()
        {
            _dtGetAutoID = _clsEmployee.GetAutoID("Bank_Code", "tb_Employee");
            if (_dtGetAutoID != null && _dtGetAutoID.Rows.Count > 0)
            {
                try
                {
                    txtEmpCode.Text = "NV.0000" +
                                      Convert.ToString(int.Parse(_dtGetAutoID.Rows[0]["Auto_ID"].ToString()) +
                                                       int.Parse(1.ToString()));
                }
                catch (Exception ex)
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", ex.Message);
                }
            }
        }
        private bool CheckInsert()
        {
            var check = false;
            var _clsEmployeeCheck = new BllEmployee();
            var dtCheck = _clsEmployeeCheck.GetEmployee(string.Empty);
            if (dtCheck != null && dtCheck.Rows.Count > 0)
            {
                foreach (DataRow dr in dtCheck.Rows)
                {
                    if (hdIDEmployee.Text != dr["Employee_Id"].ToString())
                    {
                        if (txtEmpCode.Text == dr["Bank_Code"].ToString())
                        {
                            check = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Mã nhân viên đã tồn tại !");
                        }
                    }
                }
            }
            return check;
        }
        private void Insert()
        {
            if (_flag)
            {
                InsertValue();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Bạn không được cấp quyền để sửa đổi !");
                }
                else
                {
                    UpdateValue();
                }
            }
        }
        private void InsertValue()
        {
            try
            {
                if (!CheckInsert())
                {                    
                    var obj = new EtEmployee
                    {
                        JobTitleId = String.IsNullOrEmpty(hdIDJobTitle.Text) ? (int?)null : int.Parse(hdIDJobTitle.Text),
                        BankId = String.IsNullOrEmpty(hdIDBank.Text) ? (int?)null : int.Parse(hdIDBank.Text),
                        BankCode = txtEmpCode.Text.ToUpper(),
                        FistName = HTML.clsFormat.ToTitleCase(txtFistName.Text),
                        LastName = HTML.clsFormat.ToTitleCase(txtLastName.Text),
                        FullName = HTML.clsFormat.ToTitleCase(txtLastName.Text + " " + txtFistName.Text),
                        Sex = chkEmployee_Gender.Checked ? "Nữ" : "Nam",
                        Brithday = DateTime.Parse(txtBrithday.Text),
                        HomePhone = txtHomePhone.Text,
                        CellPhone = txtCellPhone.Text,
                        Address = txtAddress.Text,
                        Email = txtEmail.Text,
                        CMND = txtCMND.Text,
                        PlaceCMND = txtPlaceCMND.Text,
                        Ethnic_Id = String.IsNullOrEmpty(hdIDEthnic_Id.Text) ? (int?)null : int.Parse(hdIDEthnic_Id.Text),
                        Districts_Id = String.IsNullOrEmpty(hdIDDistricts_Id.Text) ? (int?)null : int.Parse(hdIDDistricts_Id.Text),
                        Department_Id = String.IsNullOrEmpty(hdIDDepartment_Id.Text) ? (int?)null : int.Parse(hdIDDepartment_Id.Text),
                        Part_Id = String.IsNullOrEmpty(hdIDPart_Id.Text) ? (int?)null : int.Parse(hdIDPart_Id.Text),
                        ObjectTimekeeping = chkObjectTimekeeping.Checked,
                        EducationalLevel = txtEducationalLevel.Text,
                        Languages = txtLanguages.Text,
                        Specialize = txtSpecialize.Text,
                        InfomationLevel = txtInfomationLevel.Text,
                        BHXH = txtBHXH.Text,
                        Religion = txtReligion.Text,
                        NumberOfVacationDays = int.Parse(txtNumberOfVacationDays.Text),
                        AccountNumber = txtAccountNumber.Text,
                        DateCreateBHXH = txtDateCreateBHXH.Text == string.Empty ? string.Empty : txtDateCreateBHXH.Text,
                        DateCreate = txtDateCreate.Text == string.Empty ? string.Empty : txtDateCreate.Text,
                    };
                    //if (String.IsNullOrEmpty(FileUploadPanel.FileName))
                    //{
                    //    if (chkEmployee_Gender_Male.Checked)
                    //    {
                    //        obj.Img = pathFolderRoot + "AvatarMan/avatar-man.png";
                    //    }
                    //    else
                    //    {
                    //        obj.Img = pathFolderRoot + "AvatarWomen/avatar_women.png";
                    //    }
                    //}
                    //else
                    //{
                    //    if (chkEmployee_Gender_Male.Checked)
                    //    {
                    //        obj.Img = pathFolderRoot + "AvatarMan/" + FileUploadPanel.FileName;
                    //    }
                    //    else
                    //    {
                    //        obj.Img = pathFolderRoot + "AvatarWomen/" + FileUploadPanel.FileName;
                    //    }
                    //}
                    BllEmployee.Insert(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới nhân viên thành công !");
                    GetEmployee();
                    ClearText();
                    GetAutoID();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Thông Báo", ex.Message);
            }
        }
        private void UpdateValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtEmployee
                    {
                        EmployeeId = int.Parse(hdIDEmployee.Text),
                        JobTitleId = String.IsNullOrEmpty(hdIDJobTitle.Text) ? (int?)null : int.Parse(hdIDJobTitle.Text),
                        BankId = String.IsNullOrEmpty(hdIDBank.Text) ? (int?)null : int.Parse(hdIDBank.Text),
                        BankCode = txtEmpCode.Text.ToUpper(),
                        FistName = HTML.clsFormat.ToTitleCase(txtFistName.Text),
                        LastName = HTML.clsFormat.ToTitleCase(txtLastName.Text),
                        FullName = HTML.clsFormat.ToTitleCase(txtLastName.Text + " " + txtFistName.Text),
                        Sex = chkEmployee_Gender.Checked ? "Nữ" : "Nam",
                        Brithday = DateTime.Parse(txtBrithday.Text),
                        HomePhone = txtHomePhone.Text,
                        CellPhone = txtCellPhone.Text,
                        Address = txtAddress.Text,
                        Email = txtEmail.Text,
                        CMND = txtCMND.Text,
                        PlaceCMND = txtPlaceCMND.Text,
                        Ethnic_Id = String.IsNullOrEmpty(hdIDEthnic_Id.Text) ? (int ?) null : int.Parse(hdIDEthnic_Id.Text),
                        Districts_Id = String.IsNullOrEmpty(hdIDDistricts_Id.Text) ? (int?)null : int.Parse(hdIDDistricts_Id.Text),
                        Department_Id = String.IsNullOrEmpty(hdIDDepartment_Id.Text) ? (int?)null : int.Parse(hdIDDepartment_Id.Text),
                        Part_Id = String.IsNullOrEmpty(hdIDPart_Id.Text) ? (int?)null : int.Parse(hdIDPart_Id.Text),
                        ObjectTimekeeping = chkObjectTimekeeping.Checked,
                        EducationalLevel = txtEducationalLevel.Text,
                        Languages = txtLanguages.Text,
                        Specialize = txtSpecialize.Text,
                        InfomationLevel = txtInfomationLevel.Text,
                        BHXH = txtBHXH.Text,
                        Religion = txtReligion.Text,
                        NumberOfVacationDays = int.Parse(txtNumberOfVacationDays.Text),
                        AccountNumber = txtAccountNumber.Text,
                        DateCreate = txtDateCreate.Text,
                        DateCreateBHXH = txtDateCreateBHXH.Text,
                    };
                    if (String.IsNullOrEmpty(FileUploadPanel.FileName) && String.IsNullOrEmpty(txtPathImg.Text))
                    {
                        if (chkEmployee_Gender_Male.Checked)
                        {
                            obj.Img = pathFolderRoot + "AvatarMan/avatar-man.png";
                        }
                        else
                        {
                            obj.Img = pathFolderRoot + "AvatarWomen/avatar_women.png";
                        }
                    }
                    else
                    {
                        if (String.IsNullOrEmpty(FileUploadPanel.FileName))
                        {
                            obj.Img = txtPathImg.Text;
                        }
                        else
                        {
                            if (chkEmployee_Gender_Male.Checked)
                            {
                                obj.Img = pathFolderRoot + "AvatarMan/" + FileUploadPanel.FileName;
                            }
                            else
                            {
                                obj.Img = pathFolderRoot + "AvatarWomen/" + FileUploadPanel.FileName;
                            }
                        }
                    }
                    BllEmployee.Update(obj);
                    WindowsEmployee.Close();
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật nhân viên thành công !");
                    GetEmployee();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Thông Báo", ex.Message);
            }
        }
        #endregion

        #region[Clear-Text]
        private void ClearText()
        {
            hdIDEmployee.Text = string.Empty;
            hdIDBank.Text = string.Empty;
            hdIDJobTitle.Text = string.Empty;
            txtLastName.Text = string.Empty;
            txtFistName.Text = string.Empty;
            txtFullName.Text = string.Empty;
            txtHomePhone.Text = string.Empty;
            txtCellPhone.Text = string.Empty;
            txtBankName.Text = string.Empty;
            txtJobtitle.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtBrithday.Text = string.Empty;
            chkEmployee_Gender_Male.Checked = true;
            txtCMND.Text = string.Empty;
            txtPlaceCMND.Text = string.Empty;
            hdIDEthnic_Id.Text = string.Empty;
            hdIDDistricts_Id.Text = string.Empty;
            hdIDDepartment_Id.Text = string.Empty;
            hdIDPart_Id.Text = string.Empty;
            chkObjectTimekeeping.Checked = false;
            txtEducationalLevel.Text = string.Empty;
            txtLanguages.Text = string.Empty;
            txtSpecialize.Text = string.Empty;
            txtInfomationLevel.Text = string.Empty;
            txtBHXH.Text = string.Empty;
            txtReligion.Text = string.Empty;
            txtNumberOfVacationDays.Text = "1";
            txtAccountNumber.Text = string.Empty;
            txtDateCreate.Text = string.Empty;
            txtDateCreateBHXH.Text = string.Empty;
            SelectBoxPhoneType.Text = string.Empty;
            txtPathImg.Text = string.Empty;
            ImgEmployee.ImageUrl = string.Empty;
            //FileUploadPanel.Text = string.Empty;
        }
        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Domain, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnAdd == false)
                {
                    btnAddNew.Visible = false;
                }
                if (Is_BtnEdit == false)
                {
                    btnEdit.Visible = false;
                }
                if (Is_BtnView == false)
                {
                    PanelEmployee.Visible = false;
                }
                if (Is_BtnDel == false)
                {
                    btnDel.Visible = false;
                }
                if (Is_BtnPrint == false)
                {
                    btnExport.Visible = false;
                }
            }
        }
        private bool CheckPermisstionDobclick()
        {
            var check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Domain, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion

        #region[Upload]
        private void Upload(string path)
        {
            try
            {
                string tpl;
                tpl = "Tập tin đã tải lên : {0} <br/>Dung lượng : {1} bytes";
                if (FileUploadPanel.HasFile)
                {
                    if (CheckFileType())
                    {
                        FileUploadPanel.PostedFile.SaveAs(path + FileUploadPanel.FileName);
                        X.Msg.Show(new MessageBoxConfig
                        {
                            Buttons = MessageBox.Button.OK,
                            Icon = MessageBox.Icon.INFO,
                            Title = "Thông báo",
                            Message =
                                string.Format(tpl, FileUploadPanel.PostedFile.FileName,
                                    FileUploadPanel.PostedFile.ContentLength)
                        });
                        WindowFileUpload.Close();
                    }
                    else
                    {
                        X.Msg.Show(new MessageBoxConfig
                        {
                            Buttons = MessageBox.Button.OK,
                            Icon = MessageBox.Icon.ERROR,
                            Title = "Lỗi",
                            Message = "Tập tin " + FileUploadPanel.FileName.Split('.')[1] + " không được hỗ trợ !"
                        });
                    }
                }
                else
                {
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.ERROR,
                        Title = "Lỗi",
                        Message = "Không có tập tin được chọn"
                    });
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private bool CheckFileType()
        {
            var check = FileUploadPanel.PostedFile.FileName.EndsWith(".jpg") ||
                        FileUploadPanel.PostedFile.FileName.EndsWith(".png") ||
                        FileUploadPanel.PostedFile.FileName.EndsWith(".gif");
            return check;
        }
        private bool CheckFileNameExists(string File)
        {
            var check = false;
            var pathUrl = Server.MapPath(pathFolderRoot + File + "/");
            var files = Directory.GetFiles(pathUrl);
            foreach (var fileName in files)
            {
                var fi = new FileInfo(fileName);
                if (fi.Name == FileUploadPanel.FileName)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion
    }
}